package runningQueen.se_bigwork;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TestApp extends JPanel {

	final String picPath = "./src/main/resources/pic/";
	final String musPath = "./src/main/resources/mus/";
	// BGM
	protected BGMPlayer musicPlay = new BGMPlayer(musPath + "BGM/Dead End.mp3");
	protected SEPlayer sePlay = new SEPlayer(musPath + "SE/Button27.wav");

	public TestApp() throws IOException {
		// super(null);由于重写了UI所以null布局似乎变得不好用了【不过没太大关系
		super(new FlowLayout());
		FlowLayout layout = (FlowLayout)getLayout();
		layout.setVgap(100);
		layout.setHgap(1000);

		// ================BGM拖动条测试=================

		// 设定JSlider1，拖动的东西为自定义图标【但是图标没弄好orz
		Image img = ImageIO.read(new File(picPath + "icons/musIcon.png"));
		MySlider s1 = new MySlider(0, 100, BGMPlayer.getValue(), img);
		s1.setOpaque(false);
		// 设置主刻度，每20显示数字
		s1.setMajorTickSpacing(20);
		// 颜色什么的按照UI再来弄
		s1.setForeground(Color.white);
		s1.setThumbColor(Color.GRAY.brighter());

		// 拖动改变音量的监听
		s1.addChangeListener(new ChangeListener() {
			public void stateChanged(final ChangeEvent e) {
				if (e.getSource() instanceof JSlider) {
					double soundPercent = ((JSlider) e.getSource())
							.getValue();
					BGMPlayer.setVolume(soundPercent, true);
				}
			}
		});
		add(s1);
		
		// ================SE拖动条测试================
		
		 MySlider s2 = new MySlider(0, 100, BGMPlayer.getValue());

		// 设定JSlider1，拖动的东西为自定义图标【但是图标没弄好orz
		s2.setOpaque(false);
		// 设置主刻度，每20显示数字
		s2.setMajorTickSpacing(20);
		// 颜色什么的按照UI再来弄
		s2.setForeground(Color.white);
		s2.setThumbColor(Color.GRAY.brighter());

		// 拖动改变音量的监听
		s2.addChangeListener(new ChangeListener() {
			public void stateChanged(final ChangeEvent e) {
				if (e.getSource() instanceof JSlider) {
					double soundPercent = ((JSlider) e.getSource())
							.getValue();
					SEPlayer.setVolume(soundPercent, true);
				}
			}
		});
		add(s2);
		
		JButton test = new JButton("音效测试");
		test.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO 是否采用多线程？？？
				// 如果不采用，会卡住按钮
				// 但如果采用，会有声音和点击不同步的现象
				super.mouseClicked(e);
				new Thread(sePlay, "SEThread").start(); // 播放音乐
			}
		});
		add(test);
		
		new Thread(musicPlay, "BGMThread").start(); // 播放音乐
		 
		// 设定窗体大小
		setPreferredSize(new Dimension(800, 600));
		setOpaque(false);
	}

	// TODO main
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				createUI();
			}
		});
	}

	public static void createUI() {
		JFrame frame = new JFrame("音量刻度设置");
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		try {
			frame.getContentPane().add(new TestApp());
		} catch (IOException e) {
		}
		frame.getContentPane().setBackground(Color.GRAY);
		frame.pack();
		// 居中
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
}